package com.pan.note.config;

import com.pan.note.enumerate.TokenEnum;
import com.pan.note.dao.UserDao;
import com.pan.note.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.PostConstruct;
import java.util.List;

@Configuration
public class UserConfig {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @PostConstruct
    public void loadUser(){
        //springboot启动时加载所有用户到redis中
        List<UserDao> userDtos = userMapper.selectList(null);
        userDtos.forEach(userDto -> {
            redisTemplate.opsForHash().put(TokenEnum.USERKEY.getMsg(),userDto.getAccountId(),userDto);
        });
    }
}
